package net.cardgame.orcalecard;

import java.lang.reflect.Type;
import java.util.ArrayList;

import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.SavedListener;
import net.cardgame.orcalecard.bean.DeckBean;
import net.cardgame.orcalecard.bean.SettingApp;
import net.cardgame.orcalecard.pref.SecurePreferences;
import net.cardgame.orcalecard.utils.ConstantValue;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class SettingSpecialActivity extends Activity implements
		OnClickListener, SavedListener, OnScrollListener {
	ListSpecialAdapter adapter;
	SecurePreferences appPreferences;
	SettingApp setting;
	boolean save = true;
	ImageView button_save;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.setting_special_deck_layout);
		appPreferences = new SecurePreferences(this,
				ConstantValue.APP_PREFERENCES,
				ConstantValue.PREFERENCES_SECRET_KEY, true);
		loadSettingApp();
		findViewById(R.id.btnBack_setting_special).setOnClickListener(this);
		findViewById(R.id.btnhelp_setting_special).setOnClickListener(this);
		button_save = (ImageView) findViewById(R.id.btn_save_setting_special);
		button_save.setOnClickListener(this);
		Gson gson = new Gson();
		Type listOfDeckBean = new TypeToken<ArrayList<DeckBean>>() {
		}.getType();
		String listSaved = appPreferences
				.getString(ConstantValue.CONFIG_DECKBEAN_KEY);
		ArrayList<DeckBean> listDeckBean = gson.fromJson(listSaved,
				listOfDeckBean);
		ListView listView = (ListView) findViewById(R.id.listView_setting_special);
		adapter = new ListSpecialAdapter(this,
				R.layout.item_listview_setting_special,
				getListSpecial(listDeckBean), setting.listSpecial);
		listView.setFadingEdgeLength(0);
		listView.setAdapter(adapter);
		listView.setOnScrollListener(this);
	}

	void loadSettingApp() {
		Gson gson = new Gson();
		Type SettingAppType = new TypeToken<SettingApp>() {
		}.getType();
		if (appPreferences.containsKey(ConstantValue.SETTING_KEY)) {
			String strSetting = appPreferences
					.getString(ConstantValue.SETTING_KEY);
			// Utils.ELog("str_History", strHistory);
			setting = gson.fromJson(strSetting, SettingAppType);
		}

	}

	ArrayList<DeckBean> getListSpecial(ArrayList<DeckBean> list) {
		ArrayList<DeckBean> listtemp = new ArrayList<DeckBean>();
		for (DeckBean deckBean : list) {
			if (deckBean.percentDownloaded == -2 && deckBean.deckId != 999
					&& deckBean.deckId != 998) {
				listtemp.add(deckBean);
			}
		}
		return listtemp;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		findViewById(R.id.btnBack_setting_special).setOnClickListener(this);
		findViewById(R.id.btnhelp_setting_special).setOnClickListener(this);
		findViewById(R.id.btn_save_setting_special).setOnClickListener(this);
		switch (v.getId()) {
		case R.id.btnBack_setting_special:
			this.finish();
			break;
		case R.id.btnhelp_setting_special:
			Intent intent = new Intent(this, HelpActivity.class);
			intent.putExtra(HelpActivity.KEY_HELP_INDEX, 3);
			startActivityForResult(intent, 0);
			overridePendingTransition(R.anim.slide_in_right,
					R.anim.slide_out_left);
			break;
		case R.id.btn_save_setting_special:
			if (save) {
				setting.listSpecial = adapter.getSpecialList();
				saveSetting();
				this.finish();
			}
			break;

		default:
			break;
		}
	}

	void saveSetting() {
		Gson gson = new Gson();
		Type SettingAppType = new TypeToken<SettingApp>() {
		}.getType();
		String save = gson.toJson(setting, SettingAppType);
		appPreferences.put(ConstantValue.SETTING_KEY, save);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
	}

	@Override
	public void onSave(boolean save) {
		// TODO Auto-generated method stub
		this.save = save;
		if (!save) {
			button_save.setSelected(true);
		} else
			button_save.setSelected(false);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		if (scrollState != SCROLL_STATE_IDLE)
			adapter.setFlag(false);
		else
			adapter.setFlag(true);
		// adapter.notifyDataSetChanged();
	}
}
